package cn.dankal.basiclib;

import android.content.Context;
import androidx.multidex.MultiDex;

import com.activeandroid.app.Application;
import com.alibaba.android.arouter.launcher.ARouter;
import com.facebook.stetho.Stetho;
import com.jeremyliao.liveeventbus.LiveEventBus;
import com.jeremyliao.liveeventbus.core.LiveEvent;
import com.squareup.leakcanary.LeakCanary;
import com.squareup.leakcanary.RefWatcher;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;
import com.tencent.stat.StatConfig;
import com.tencent.stat.StatService;
import com.tencent.tauth.Tencent;

import cn.dankal.basiclib.util.AppUtils;
import cn.dankal.basiclib.util.DensityAdaptationUtils;
import cn.dankal.basiclib.util.GlobalEventListener;
import cn.dankal.basiclib.util.SharePreferenceManager;
import cn.dankal.basiclib.util.StringUtil;
import cn.dankal.basiclib.widget.loadsir.EmptyCallback;
import cn.dankal.basiclib.widget.loadsir.LoadingCallback;
import cn.dankal.basiclib.widget.loadsir.RetryCallback;
import cn.dankal.basiclib.widget.loadsir.core.LoadSir;
import cn.jpush.im.android.api.JMessageClient;


/**
 * @author Dankal Android Developer
 * @since 2018/5/9
 */

public class DankalApplication extends com.activeandroid.app.Application {
    //是否是开发环境
    public static final boolean isDev = true;
    //这个是用于绑定的时候用的，步骤太多了，不用传来传去。不用于其他地方
    public static String openId;
    public static final String CONV_TITLE = "conv_title";

    private static DankalApplication context;

    private static DankalApplication defaultApp = null;

    private static final String APP_ID = "wxda359b75ef2e3206";

    public static final String TARGET_ID = "targetId";
    public static final String ATUSER = "atuser";
    public static final String TARGET_APP_KEY = "targetAppKey";

    public static final String GROUP_ID = "groupId";

    // IWXAPI 是第三方app和微信通信的openApi接口
    private IWXAPI api;

    private Tencent myTencent;

    private RefWatcher refWatcher;

    @Override
    public void onCreate() {
        super.onCreate();
        context = this;
        defaultApp = this;

        initARouter();

        if (cn.dankal.basiclib.BuildConfig.DEBUG) {
            initStetho();
        }

        initLoadSir();
        regToWx();
        //适配方案
        DensityAdaptationUtils.setDensity(context, 375);
        AppUtils.init(context);

        refWatcher = setupLeakCanary();

        LiveEventBus.config().autoClear(true);
        // 打开Logcat输出，上线时，一定要关闭
        StatConfig.setDebugEnable(isDev);
        // 注册activity生命周期，统计时长
        StatService.registerActivityLifecycleCallbacks(this);
    }


    private RefWatcher setupLeakCanary() {
        if (LeakCanary.isInAnalyzerProcess(this)) {
            return RefWatcher.DISABLED;
        }
        return LeakCanary.install(this);
    }

    public static RefWatcher getRefWatcher(Context context) {
        DankalApplication leakApplication = (DankalApplication) context.getApplicationContext();
        return leakApplication.refWatcher;
    }

    public static DankalApplication instance() {
        return defaultApp;
    }

    /**
     * 初始化阿里路由
     */
    private void initARouter() {
        if (BuildConfig.DEBUG) {
            // 打印日志
            ARouter.openLog();
            // 开启调试模式(如果在InstantRun模式下运行，必须开启调试模式！线上版本需要关闭,否则有安全风险)
            ARouter.openDebug();
        }
        ARouter.init(this);
    }

    private void initLoadSir() {
        LoadSir.beginBuilder()
                .addCallback(new RetryCallback())
                .addCallback(new EmptyCallback())
                .addCallback(new LoadingCallback())
//                .setDefaultCallback(LoadingCallback.class)
                .commit();
    }

    public static Context getContext() {
        return context;
    }

    /**
     * facebook出品辅助开发工具
     */
    private void initStetho() {
        Stetho.initializeWithDefaults(this);
    }

    @Override
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(base);
        MultiDex.install(this);
    }

    private static boolean login;

    public static void setLogin(boolean login) {
        DankalApplication.login = login;
    }

    public static boolean isLogin() {
        return login || (DKUserManager.getUserInfo() != null
                && StringUtil.isValid(DKUserManager.getUserInfo().getUserUuid()));
    }

    private void regToWx() {

    }

    public IWXAPI getIWXAPI() {
        // 通过WXAPIFactory工厂，获取IWXAPI的实例
        api = WXAPIFactory.createWXAPI(this, APP_ID, isDev);

        // 将应用的appId注册到微信
        api.registerApp(APP_ID);
        return api;
    }

    public Tencent getTencent() {

        //传入参数APPID
        myTencent = Tencent.createInstance("1108145855", getApplicationContext());
        return myTencent;
    }
}
